using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.TextEditor.Document;
using CodeGenerator;
using CodeGenerator.Classes;

namespace CodeGenerator
{
    public partial class frmScriptSQL : Form
    {
        public static string lConnectionString = "";
        public static string lDataBase = "";

        public frmScriptSQL()
        {
            InitializeComponent();
        }

        public frmScriptSQL(string ConnStr, string DB)
        {
            InitializeComponent();

            lConnectionString = ConnStr;
            lDataBase = DB;

            CreateTree();

        }

        private void CreateTree()
        {
            //TV1.Nodes[0].Nodes[0].Nodes.Clear();
            //            TV1.Nodes[1].Nodes.Clear();

            CodeGenerator.Classes.DataBaseInformation DI = new CodeGenerator.Classes.DataBaseInformation(lConnectionString);
            for (int i = 0; i < DI.LoadTable().Count; i++)
            {
                //TV1.Nodes[0].Nodes[0].Nodes.Add(DI.LoadTable()[i].ToString());
                //TV1.Nodes[0].Nodes[0].Nodes[i].ImageIndex = 2;
                //TV1.Nodes[0].Nodes[0].Nodes[i].SelectedImageIndex = 2;
                //TV1.Nodes[0].Nodes[0].Nodes[i].ContextMenuStrip = contextMenuStrip1;

                listBox1.Items.Add(DI.LoadTable()[i].ToString());
            }

            //for (int j = 0; j < TV1.Nodes[0].Nodes[0].Nodes.Count; j++)
            //{
            //    for (int k = 0; k < DI.LoadFileds(TV1.Nodes[0].Nodes[0].Nodes[j].Text).Count; k++)
            //    {
            //        TV1.Nodes[0].Nodes[0].Nodes[FindID(TV1.Nodes[0].Nodes[0].Nodes[j].Text)].Nodes.Add(DI.LoadFileds(TV1.Nodes[0].Nodes[0].Nodes[j].Text)[k].ToString());
            //        TV1.Nodes[0].Nodes[0].Nodes[FindID(TV1.Nodes[0].Nodes[0].Nodes[j].Text)].Nodes[k].ImageIndex = 4;
            //        TV1.Nodes[0].Nodes[0].Nodes[FindID(TV1.Nodes[0].Nodes[0].Nodes[j].Text)].Nodes[k].SelectedImageIndex = 4;
            //    }
            //}
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > -1)
            {
                //string _value = listBox1.Text; //Gets the value of  items in list.

                string _text = listBox1.Text;  // Gets the Text of items in the list.  

                //ListBox   item = new ListBox(); //create a list item

                //item.Text = _text;               //Assign the values to list item   

                //item.SelectedValue = _value;
                listBox2.Items.Add(_text); //Add the list item to the selected list of employees   

                listBox1.Items.Remove(_text); //Remove the details from employee list   

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (listBox2.SelectedIndex > -1)
            {
                //string _value = listBox1.Text; //Gets the value of  items in list.

                string _text = listBox2.Text;  // Gets the Text of items in the list.  

                //ListBox   item = new ListBox(); //create a list item

                //item.Text = _text;               //Assign the values to list item   

                //item.SelectedValue = _value;
                listBox1.Items.Add(_text); //Add the list item to the selected list of employees   

                listBox2.Items.Remove(_text); //Remove the details from employee list   

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int _count = listBox1.Items.Count;
            if (_count != 0)
            {
                for (int i = 0; i < _count; i++)
                {
                    //ListItem item = new ListItem();
                    //item.Text = lstSelectedEmployees.Items[i].Text;
                    string item = listBox1.Items[i].ToString();
                    listBox2.Items.Add(item);
                }
            }

            listBox1.Items.Clear();//clear the items  
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int _count = listBox2.Items.Count;
            if (_count != 0)
            {
                for (int i = 0; i < _count; i++)
                {
                    //ListItem item = new ListItem();
                    //item.Text = lstSelectedEmployees.Items[i].Text;
                    string item = listBox2.Items[i].ToString();
                    listBox1.Items.Add(item);
                }
            }

            listBox2.Items.Clear();//clear the items  
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            //if (checkBox1.Checked)
            //{

            try
            {
                for (int i = 0; i < listBox2.Items.Count; i++)
                {
                    Classes.File f = new CodeGenerator.Classes.File();
                    Classes.GeneratorClass c = new CodeGenerator.Classes.GeneratorClass(lConnectionString, listBox2.Items[i].ToString());

                    if (chkScriptSql.Checked)
                    {
                        if (chkUpdate.Checked)
                            f.Save(listBox2.Items[i].ToString() + "_Update", textBox1.Text, c.ReturnSQLUpdate(), "sql");

                        if (chkInsert.Checked)
                            f.Save(listBox2.Items[i].ToString() + "_Insert", textBox1.Text, c.ReturnSQLInsert(), "sql");

                        if (chkDelete.Checked)
                            f.Save(listBox2.Items[i].ToString() + "_Delete", textBox1.Text, c.ReturnSQLDelete(), "sql");

                        if (chkSelect.Checked)
                        {
                            f.Save(listBox2.Items[i].ToString() + "_SelectAll", textBox1.Text, c.ReturnSQLSelect(), "sql");

                            f.Save(listBox2.Items[i].ToString() + "_SelectByID", textBox1.Text, c.ReturnSQLSelectByPK(), "sql");
                        }

                        //if (checkBox6.Checked)
                        //    f.Save(listBox2.Items[i].ToString(), textBox1.Text, c.ReturnClass("C#"), "cs");

                    }




                    //Classes.File f = new CodeGenerator.Classes.File();
                    //Classes.GeneratorClass c = new CodeGenerator.Classes.GeneratorClass(lConnectionString, TV1.Nodes[0].Nodes[0].Nodes[i].Text);
                    //f.Save(TV1.Nodes[0].Nodes[0].Nodes[i].Text, folderBrowserDialog1.SelectedPath, c.ReturnClass("C#"), "cs");

                    MessageBox.Show("Berhasil...");

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            //}
        }

        private void button6_Click(object sender, EventArgs e)
        {
            

            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (chkScriptSql.Checked)
            {
                chkInsert.Visible = true;
                chkDelete.Visible = true;
                chkUpdate.Visible = true;
                chkSelect.Visible = true;

                chkInsert.Checked = true;
                chkDelete.Checked = true;
                chkUpdate.Checked = true;
                chkSelect.Checked = true;
            }
            else
            {
                chkInsert.Visible = false;
                chkDelete.Visible = false;
                chkUpdate.Visible = false;
                chkSelect.Visible = false;
            }
        }

        private void Utama_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Hide();
        }

        private void checkBox7_CheckedChanged(object sender, EventArgs e)
        {
           
        }

        private void chkNameSpaceEntity_CheckedChanged(object sender, EventArgs e)
        {
            
        }


    }
}